//console.cpp

#include "precompiled_header.h"
#include "console.h"

void CreateDebugConsole(void)
{
    HDC hDC;
    WNDCLASS wc;
    RECT rect;
    const char* wndClassName = "Engine Debug Console";
    int style = WS_POPUPWINDOW | WS_CAPTION | WS_MINIMIZEBOX;
    int screenWidth, screenHeight;

    memset(&wc, 0, sizeof(wc));

    win32con.wndproc = (WNDPROC)ConsoleWndProc;

    wc.lpfnWndProc  = (WNDPROC)ConsoleWndProc;
    wc.hInstance    = win32.hInstance;
    //wc.hIcon      = ;
    wc.hCursor      = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (struct HBRUSH__*)COLOR_WINDOW;
    wc.lpszClassName = wndClassName;

    if(!RegisterClass(&wc))
        return;

    rect.left = 0;
    rect.top = 0;
    rect.right = 600;
    rect.bottom = 450;
    AdjustWindowRect(&rect, style, FALSE);

    hDC = GetDC(GetDesktopWindow());
    screenWidth = GetDeviceCaps(hDC, HORZRES);
    screenHeight = GetDeviceCaps(hDC, VERTRES);
    ReleaseDC(GetDesktopWindow(), hDC);

    win32con.hWnd = CreateWindowEx( 0,                          //dwExStyle
                                    wndClassName,               //lpClassName
                                    wndClassName,               //lpWindowName
                                    style,                      //dwStyle
                                    (screenWidth - 600) / 2,    //X,
                                    (screenHeight - 450) / 2,   //Y,
                                    rect.right - rect.left,     //nWidth
                                    rect.bottom - rect.top,     //nHeight
                                    NULL,                       //hWndParent
                                    NULL,                       //hMenu
                                    win32.hInstance,            //hInstance
                                    NULL);                      //lpParam

    if(win32con.hWnd == NULL)
        return;

    //Now show it
    ShowWindow( win32con.hWnd, SW_SHOWDEFAULT);
    UpdateWindow( win32con.hWnd );
    SetForegroundWindow( win32con.hWnd );

}

void ShutdownDebugConsole(void)
{
    if(win32con.hWnd)
    {
        ShowWindow(win32con.hWnd, SW_HIDE);
        CloseWindow(win32con.hWnd);
        DestroyWindow(win32con.hWnd);
        win32con.hWnd = NULL;
    }
}

LRESULT WINAPI ConsoleWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_CLOSE:
        PostQuitMessage(0);
    }

    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}